package com.chenlonghai.sbmybatis.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chenlonghai.sbmybatis.entity.Banner;
import com.chenlonghai.sbmybatis.entity.ResultModel;
import com.chenlonghai.sbmybatis.imgconfig.WebAppConfig;

@Controller
@RequestMapping(value="/img")
public class HandleImagesController {
	@Value("${cbs.location}")
    private String locationPath;
	//跳转到上传文件的页面
    @RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
    public String goUploadImg() {

        //跳转到 templates 目录下的 uploadimg.html
        return "uploadimg";
    }

    //处理文件上传

    @RequestMapping(value="/addBanner", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> addBanner(MultipartFile file,
            HttpServletRequest request){
		

		Map<String, Object>modelMap = new HashMap<>();

		System.out.println("你才次"+  file);
		

	     if (!file.isEmpty()) {

	 		    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
	            String saveFileName = df.format(new Date()) + file.getOriginalFilename();
	            
	          
	            System.out.println("1->"+saveFileName);
	            File saveFile = new File(locationPath + saveFileName);
	            if (!saveFile.getParentFile().exists()) {
	                saveFile.getParentFile().mkdirs();
	            }
	            try {
	                System.out.println("2->"+saveFileName);
	                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
	                out.write(file.getBytes());
	                out.flush();
	                out.close();
	                System.out.println("3->"+saveFileName);
	                
	           
	             	ResultModel<String>result = new ResultModel<String>("/images/"+saveFileName,modelMap);
	                return result.getResltMap();
	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            } catch (IOException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            }
	        } else {
	        	ResultModel<String>result = new ResultModel<String>(null,modelMap);
                return result.getResltMap();

	    }

	}
    
  //处理文件上传

    @RequestMapping(value="/addcase", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> addcase(MultipartFile file,
            HttpServletRequest request){
		

		Map<String, Object>modelMap = new HashMap<>();

		System.out.println("你才次"+  file);
		

	     if (!file.isEmpty()) {

	 		    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
	            String saveFileName = df.format(new Date()) + file.getOriginalFilename();
	            
	          
	            System.out.println("1->"+saveFileName);
	            File saveFile = new File(locationPath+"case/" + saveFileName);
	            if (!saveFile.getParentFile().exists()) {
	                saveFile.getParentFile().mkdirs();
	            }
	            try {
	                System.out.println("2->"+saveFileName);
	                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
	                out.write(file.getBytes());
	                out.flush();
	                out.close();
	                System.out.println("3->"+saveFileName);
	                
	           
	             	ResultModel<String>result = new ResultModel<String>("/images/"+"case/"+saveFileName,modelMap);
	                return result.getResltMap();
	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            } catch (IOException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            }
	        } else {
	        	ResultModel<String>result = new ResultModel<String>(null,modelMap);
                return result.getResltMap();

	    }

	}
    
    
    //处理文件上传

    @RequestMapping(value="/addOpinion", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> addOpinion(MultipartFile file,
            HttpServletRequest request){
		

		Map<String, Object>modelMap = new HashMap<>();

		System.out.println("你才次"+  file);
		

	     if (!file.isEmpty()) {

	 		    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
	            String saveFileName = df.format(new Date()) + file.getOriginalFilename();
	            
	          
	            System.out.println("1->"+saveFileName);
	            File saveFile = new File(locationPath+"opinion/" + saveFileName);
	            if (!saveFile.getParentFile().exists()) {
	                saveFile.getParentFile().mkdirs();
	            }
	            try {
	                System.out.println("2->"+saveFileName);
	                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
	                out.write(file.getBytes());
	                out.flush();
	                out.close();
	                System.out.println("3->"+saveFileName);
	                
	           
	             	ResultModel<String>result = new ResultModel<String>("/images/"+"opinion/"+saveFileName,modelMap);
	                return result.getResltMap();
	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            } catch (IOException e) {
	                e.printStackTrace();
	                ResultModel<String>result = new ResultModel<String>(null,modelMap);
	                return result.getResltMap();
	            }
	        } else {
	        	ResultModel<String>result = new ResultModel<String>(null,modelMap);
                return result.getResltMap();

	    }

	}
}
    
//    /**
//     * 多文件上传
//     *
//     * @param request
//     * @return
//     */
//    @PostMapping("/uploadFiles")
//    @ResponseBody
//    public String uploadFiles(HttpServletRequest request) throws IOException {
//        File savePath = new File(request.getSession().getServletContext().getRealPath("/upload/"));
//        if (!savePath.exists()) {
//            savePath.mkdirs();
//        }
//        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
//        MultipartFile file = null;
//        BufferedOutputStream stream = null;
//        for (int i = 0; i < files.size(); ++i) {
//            file = files.get(i);
//            if (!file.isEmpty()) {
//                try {
//                    byte[] bytes = file.getBytes();
//                    File saveFile = new File(savePath, file.getOriginalFilename());
//                    stream = new BufferedOutputStream(new FileOutputStream(saveFile));
//                    stream.write(bytes);
//                    stream.close();
//                } catch (Exception e) {
//                    if (stream != null) {
//                        stream.close();
//                        stream = null;
//                    }
//                    return "第 " + i + " 个文件上传有错误" + e.getMessage();
//                }
//            } else {
//                return "第 " + i + " 个文件为空";
//            }
//        }
//        return "所有文件上传成功";
//
//}